package org.jeecg.modules.exZbidOpeningArrange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.exBiddingRoom.entity.ExBiddingRoom;
import org.jeecg.modules.exBiddingRoom.service.IExBiddingRoomService;
import org.jeecg.modules.exOperationRecord.entity.ExOperationRecord;
import org.jeecg.modules.exOperationRecord.service.IExOperationRecordService;
import org.jeecg.modules.exProject.constant.ExProjectStepEnum;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.service.IExProjectService;
import org.jeecg.modules.exZbidOpeningArrange.entity.ExOpeningArrangeBiddingRoomRecord;
import org.jeecg.modules.exZbidOpeningArrange.entity.ExZbidChangdiandbd;
import org.jeecg.modules.exZbidOpeningArrange.entity.ExZbidOpeningArrange;
import org.jeecg.modules.exZbidOpeningArrange.mapper.ExOpeningArrangeBiddingRoomRecordMapper;
import org.jeecg.modules.exZbidOpeningArrange.mapper.ExZbidChangdiandbdMapper;
import org.jeecg.modules.exZbidOpeningArrange.mapper.ExZbidOpeningArrangeMapper;
import org.jeecg.modules.exZbidOpeningArrange.service.IExZbidOpeningArrangeService;
import org.jeecg.modules.exZbidOpeningArrange.vo.ExZbidOpeningArrangeParam;
import org.jeecg.modules.gcProject.vo.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Description: 场地预约
 * @Author: jeecg-boot
 * @Date:   2024-08-31
 * @Version: V1.0
 */
@Service
public class ExZbidOpeningArrangeServiceImpl extends ServiceImpl<ExZbidOpeningArrangeMapper, ExZbidOpeningArrange> implements IExZbidOpeningArrangeService {

	@Autowired
	private ExZbidOpeningArrangeMapper exZbidOpeningArrangeMapper;
	@Autowired
	private ExZbidChangdiandbdMapper exZbidChangdiandbdMapper;
	@Autowired
	private IExProjectService iExProjectService;
	@Autowired
	private ExOpeningArrangeBiddingRoomRecordMapper exOpeningArrangeBiddingRoomRecordMapper;
	@Autowired
	private IExOperationRecordService exOperationRecordService;
	@Autowired
	private IExBiddingRoomService exBiddingRoomService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ExZbidOpeningArrange exZbidOpeningArrange, List<ExZbidChangdiandbd> exZbidChangdiandbdList, List<ExOpeningArrangeBiddingRoomRecord> roomRecordList) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		exZbidOpeningArrange.setCreateBy(sysUser.getId());
		exZbidOpeningArrange.setCreateTime(new Date());
		ExBiddingRoom exBiddingRoom = exBiddingRoomService.getById(exZbidOpeningArrange.getBiddingRoomId());
		exZbidOpeningArrange.setBidSectionCode(exBiddingRoom.getRoomCode());
		exZbidOpeningArrange.setBiddingRoomName(exBiddingRoom.getRoomName());
		ExProject exProject = iExProjectService.getById(exZbidOpeningArrange.getProjectId());
	 	exZbidOpeningArrange.setProjectCode(exProject.getProjectCode());
		exZbidOpeningArrange.setProjectName(exProject.getProjectName());
		exZbidOpeningArrange.setProjectType("JSGC");//todo 暂时固定
		exZbidOpeningArrangeMapper.insert(exZbidOpeningArrange);
		if(exZbidChangdiandbdList!=null && exZbidChangdiandbdList.size()>0) {
			for(ExZbidChangdiandbd entity:exZbidChangdiandbdList) {
				//外键设置
				entity.setFieldnameid(exZbidOpeningArrange.getId());
				entity.setTenderProjectId(exZbidOpeningArrange.getProjectId());
				entity.setCreateTime(new Date());
				exZbidChangdiandbdMapper.insert(entity);
			}
		}
		if(CollectionUtils.isNotEmpty(roomRecordList)){
			for(ExOpeningArrangeBiddingRoomRecord entity:roomRecordList) {
				entity.setFieldId(exZbidOpeningArrange.getId());
				entity.setCreateTime(new Date());
				exOpeningArrangeBiddingRoomRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ExZbidOpeningArrange exZbidOpeningArrange,List<ExZbidChangdiandbd> exZbidChangdiandbdList) {
		exZbidOpeningArrangeMapper.updateById(exZbidOpeningArrange);
		
		//1.先删除子表数据
		exZbidChangdiandbdMapper.deleteByMainId(exZbidOpeningArrange.getId());
		
		//2.子表数据重新插入
		if(exZbidChangdiandbdList!=null && exZbidChangdiandbdList.size()>0) {
			for(ExZbidChangdiandbd entity:exZbidChangdiandbdList) {
				//外键设置
				entity.setFieldnameid(exZbidOpeningArrange.getId());
				entity.setTenderProjectId(exZbidOpeningArrange.getProjectId());
				entity.setUpdateTime(new Date());
				exZbidChangdiandbdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		exZbidChangdiandbdMapper.deleteByMainId(id);
		exZbidOpeningArrangeMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			exZbidChangdiandbdMapper.deleteByMainId(id.toString());
			exZbidOpeningArrangeMapper.deleteById(id);
		}
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean audit(ExZbidOpeningArrangeParam param) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		ExZbidOpeningArrange byId = this.getById(param.getId());
		if (byId == null) {
			throw new JeecgBootException("未找到对应数据");
		}
		byId.setStatus(param.getStatus());
		byId.setUpdateTime(new Date());
		byId.setUpdateBy(sysUser.getUsername());
		this.updateById(byId);
		ExOperationRecord exOperationRecord = new ExOperationRecord();
		exOperationRecord.setEntryId(byId.getId());
		exOperationRecord.setModule("exZbidOpeningArrange");
		exOperationRecord.setOperateTime(new Date());
		exOperationRecord.setOperateResult(StatusEnum.SUCCESS.getCode().equals(param.getStatus()) ? "1" : "2");//1 通过 2驳回
		exOperationRecord.setOperateOpinion(param.getOperateOpinion());
		exOperationRecord.setCategory("1");
		exOperationRecord.setOperator(sysUser.getRealname());
		exOperationRecordService.save(exOperationRecord);

		// 审核通过则更新项目信息的步骤为场地预约
		ExProject exProject = iExProjectService.getById(byId.getProjectId());
		if (null != exProject && (null == exProject.getStep() || exProject.getStep() < ExProjectStepEnum.CDYY.getValue()) && StatusEnum.SUCCESS.getCode().equals(param.getStatus())) {
			exProject.setStep(ExProjectStepEnum.CDYY.getValue());
			iExProjectService.updateById(exProject);
		}
		return true;
	}

	@Override
	public ExZbidOpeningArrange getBySectionId(String sectionId) {
		LambdaQueryWrapper<ExZbidOpeningArrange> query = new LambdaQueryWrapper<ExZbidOpeningArrange>()
				.like(ExZbidOpeningArrange::getExSectionIds, sectionId);
		List<ExZbidOpeningArrange> list = this.list(query);
		return list.size() > 0 ? list.get(0) : null;
	}

}
